﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Dynamic;
using System.Transactions;
using PagedList;
using Sovigaz.DataProvider;
using Utilities;

namespace Sovigaz.Permission.Repositories
{
    public class InOutLineRepository : GenericRepository
    {
        public InOutLineRepository()
            : base("InOutLineID")
        {
        }

        //public List<InOutLine> GetByInOutID(int id)
        //{
        //    return _context.InOutLines.Where(p => p.InOutID == id && !p.IsDeleted).ToList();
        //}

        public List<InOutLineComplex> GetByInOutID(int id)
        {
            var query = _context.InOutLines.Where(p => p.InOutID == id && !p.IsDeleted)
                .Join(_context.Products, i => i.ProductID, p => p.ProductID, (i, p) => new { i, p })
                .Join(_context.Orders, ip => ip.i.OrderID, o => o.OrderID, (ip, o) => new { ip, o })
                .Join(_context.Partners, ipo => ipo.ip.i.PartnerID, pa => pa.PartnerID,
                (ipo, pa) => new InOutLineComplex
                {
                    InOutLineID=ipo.ip.i.InOutLineID,
                    InOutID = ipo.ip.i.InOutID,
                    ProductID = ipo.ip.i.ProductID,
                    OrderID = ipo.ip.i.OrderID,
                    PartnerID = ipo.ip.i.PartnerID,
                    Barcode=ipo.ip.p.Barcode,
                    ProductCode = ipo.ip.p.ProductCode,
                    OrderCode=ipo.o.OrderCode,
                    PartnerName=pa.PartnerName,
                });
            return query.ToList();
        }

    }
}
